<?php


namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller{

	//前后端分离

	//添加用户界面
	public function add(){$this->display();}


	//处理提交请求
	public function add_act(){
		$data = $_POST;
    
        $data['user_name'] = $_POST['user_name'];
        $data['user_pass'] = md5($_POST['user_pass']);
        $data['user_eamil'] = $_POST['user_email'];
        $data['user_address'] = $_POST['user_address'];
        $data['user_phone'] = $_POST['user_phone'];
        $data['user_sex'] = $_POST['user_sex'];
        $data['user_age'] = $_POST['user_age'];
        $data['status'] = 1;

       
        //往数据库写入数据
        $res = M('User')->add($data);
        
        if($res>0){
			$json = array('status'=>1,'msg'=>'添加成功');
		}
		else{
			$json = array('status'=>0,'msg'=>'添加失败');
		}
			
		$this->ajaxReturn($json);

	}

	// b/s
	// b  browser  浏览器
	// s  server   服务器

	//展示模板，模板是没有数据的，要通过js再一次请求服务器才能拿到数据
    public function user_list(){
	    $this->display();

	}

	//请求用户列表
	public function get_user_list(){
		//把satus等于-1的数据不显示在列表
		$condition['status'] = array('neq',-1);

   		$user = M('user')->where($condition)->select();

   		$this->ajaxReturn($user);   //数据以json形式返回
	}


    //修改用户信息
	public function user_update(){
		if(IS_AJAX){

			$data = $_POST;
			unset($data['id']);
			$res = M('User')->where(array('id'=>$_POST['id']))->save($data);	

			if($res>0){
				
				$this->ajaxReturn(array('status'=>1,'msg'=>'修改成功'));
			}else{
				
				$this->ajaxReturn(array('status'=>0,'msg'=>'修改失败'));
			}	

		}else{
			$id = I('id');
			$arr = array('id'=>$id);

			//$user = M('user')->where(array('id'=>$_GET['id']))->find();
			$user = M('user')->where($arr)->find();
		
	        $this->assign('user',$user);
	        $this->display();

		}
        
	}


	//修改删除、禁用状态
	public function change_status(){
		$id = $_POST['id'];
		$arr1 = array('id'=>$id);


		$status = $_POST['status'];
		$arr2 = array('status'=>$status);
		
		$res = M('User')->where($arr1)->save($arr2);
		
		if($res>0){
			$this->ajaxReturn(array('status'=>1,'msg'=>'操作成功'));
		}else{
			$this->ajaxReturn(array('status'=>0,'msg'=>'操作失败'));
		}

	}

}



















































